package com.itzh.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {

        String str = "hello,嗨皮";

        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\周志军\\Desktop\\file01.txt");

        //通过输出流获取对应的文件channel
        FileChannel fileChannel = fileOutputStream.getChannel();
        
        //创建一个缓存区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        //把 str 放入到byteBuffer
        byteBuffer.put(str.getBytes());

        //对 byteBuffer 进行翻转
        byteBuffer.flip();

        //将 byteBuffer 写入到  fileChannel
        fileChannel.write(byteBuffer);

        fileOutputStream.close();
    }
}
